var db = require('./db');
var mongodb = new db();
var ObjectID = require("mongodb").ObjectID;
var icdIndex = require('../models/icdIndex.js');

//基本实现
exports.create= function createCD(filter,callback){
    var cd = {
        user : filter.user,
        ccd_id : filter.ccd_id
    }
    mongodb.getCollection('icd',function(collection){
        collection.findOne(filter, function(err, doc) {
            if(err != null){
                return callback(err, doc);
            }
            if(doc != null){
                err = "already Exist";
                return callback(err, doc);
            }
            console.log("maybe a bug");
            console.log(filter);
            collection.insert(filter, {safe: true}, function(err, doc) {
                icdIndex.update(doc[0],function(err2,doc2){
                    return callback(err, doc);
                });
            });
        });
    });
};

//get正常
exports.get = function getCD(filter,callback){
    mongodb.getCollection('icd',function(collection){
        collection.findOne(filter, function(err, doc) {
             if(err != null){
                return callback(err, doc);
             }
             if(doc === null){
                 err = "not Exist";
                 return callback(err, doc);
             }
             return callback(err, doc);
        });
    });
};

//测试通过
exports.remove = function removeIcd(filter,callback){
    mongodb.getCollection('icd',function(collection){
        collection.remove(filter[0],function(err){
            if (err) console.warn(err.message);
            else {
                console.log('icd successfully removed');
                if(filter[0] != null) icdIndex.delete(filter[2],filter[3],function(){});
            }
            return callback();
        });
    });
};

//update,newOne可以改成如{"$set":{cd:123}}形式
//{upsert:true}为前者查找不着时，自动添加filter再执行后续步骤
//多层数据可以使用多层嵌套
//测试通过
exports.update = function update(filter,newOne,callback){
    mongodb.getCollection('icd',function(collection){
        collection.update(filter, newOne,{safe:true},function(err) {
            if (err) console.warn(err.message);
            else console.log('icd successfully updated');
        });
    });
};

//unset

//查找并更换
exports.findAndModify = function findAndModify(filter,sort,newOne,callback){
    mongodb.getCollection('icd',function(collection){
        collection.findAndModify(filter,sort,newOne,function(){
            if (err) console.warn(err.message);
            else console.dir(object);  // undefined if no matching object exists.
        });
    });
};


//

exports.copy= function copyElem(id,element,callback){
    console.log(element);
    mongodb.getCollection('icd',function(collection){
        collection.update(id,{"$set":element},{safe:true},function(err,count) {
            if (err) console.warn(err.message);
            else console.log('icd successfully copyed');
            return callback(err,count);
        });
    });
};


exports.add= function addElem(filter,callback){
    var x = filter[0];
    var y ={};
    filter[1] +='._nor';
    y[filter[1]]=1;
    console.log(y);
    mongodb.getCollection('icd',function(collection){
       collection.update( x,{"$inc":y},{safe:true},function(err,number) {
        if (err) console.warn(err.message);
            else console.log('icd successfully updated');
            callback(err,number);
        });
    });
};

exports.add2= function addElem2(filter,callback){
    var x = filter[0];
    var y ={};
    filter[1] +='._nor';
    filter[2] +='._nor';
    y[filter[1]]=1;
    y[filter[2]]=1;
    console.log(y);
    mongodb.getCollection('icd',function(collection){
        collection.update( x,{"$inc":y},{safe:true},function(err,number) {
            if (err) console.warn(err.message);
            else console.log('icd successfully updated');
            callback(err,number);
        });
    });
};
//
exports.delete= function deleteElem(filter,callback){
    var x = filter[0];
    var y ={};
    y[filter[1]]=1;
    console.log(y);
    mongodb.getCollection('icd',function(collection){
        collection.update(x,{"$unset":y},{safe:true},function(err,count) {
            if (err) console.warn(err.message);
            else console.log('successfully deleted');
            return callback(err,count);
        });
    });
};

exports.revise = function revise(filter,callback){
    filter[1] = '{"'+filter[1]+'._nor":1';

    mongodb.getCollection('icd',function(collection){
        collection.update(filter[0],{"$set":filter},{safe:true},function(err) {
            if (err) console.warn(err.message);
            else console.log('icd successfully revised');
        });
    });
};

exports.generateFilter = function generateFilter(type,stateArray,callback){
    switch(type){
        case 'cd' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'cd.'+stateArray.cd];
            break;
        case 'class':
            var filter = [{'_id':ObjectID(stateArray._id)},
                'class.'+stateArray.class,
                'class.'+stateArray.class+'._value.'+stateArray.class];
            break;
        case 'classValue':
            var filter = [{'_id':ObjectID(stateArray._id)},
                'class.'+stateArray.class+'._value.'+stateArray.classValue];
            break;
        case 'attribute' :
            var filter = [{'_id':ObjectID(stateArray._id)},
            'class.'+stateArray.class+'.'+stateArray.attribute,
            'class.'+stateArray.class+'.'+stateArray.attribute+'._value.'+stateArray.attribute];
            break;
        case 'attributeValue' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'class.'+stateArray.class+'.'+stateArray.attribute+'._value.'+stateArray.attributeValue];
            break;
        case 'attributeElem' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'class.'+stateArray.class+'.'+stateArray.attribute+'.'+stateArray.attributeElem];
                //'class.'+stateArray.class+'.'+stateArray.attribute+'.'+stateArray.attributeElem+'.'+stateArray.attributeElemValue];
            break;
        case 'attributeElemValue' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'class.'+stateArray.class+'.'+stateArray.attribute+'.'+stateArray.attributeElem+'.'+stateArray.attributeElemValue,
                'class.'+stateArray.class+'.'+stateArray.attribute+'.'+stateArray.attributeElem];
            break;
        case 'relation' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'relation.'+stateArray.relation,
                'relation.'+stateArray.relation+'._value.'+stateArray.relation];
            break;
        case 'relationType' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'relation.'+stateArray.relation+'.'+stateArray.relationType];
            break;
        case 'relationElemValue' :
            var filter = [{'_id':ObjectID(stateArray._id)},
                'relation.'+stateArray.relation+'.'+stateArray.relationType+'.'+stateArray.relationElem+'.'+stateArray.relationElemValue];
            break;
        default : break;
    }
    return callback(filter);
};
//不完整的信息
exports.generateElement = function generateElement(type,stateArray,name,sub,callback){
    var id = {'_id':ObjectID(stateArray._id)};
    switch(type){
        case 'cd' :
            var element={};
            var key = 'cd.'+name;
            element[key]=sub;
            break;
        case '_description':
            var element={};
            var key = 'cd.'+stateArray['cd']+'._description';
            element[key]=sub;
            break;
        case 'class':
            var key = 'class.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'classValue':
            var key = 'class.'+stateArray['class']+'._value.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'attribute' :
            var key = 'class.'+stateArray['class']+'.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'attributeValue' :
            var key = 'class.'+stateArray['class']+'.'+stateArray['attribute']+'._value.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'attributeElem' :
            var key = 'class.'+stateArray['class']+'.'+stateArray['attribute']+'.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'attributeElemValue' :
            var key = 'class.'+stateArray['class']+'.'+stateArray['attribute']+'.'+stateArray['attributeElem']+'.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'relation' :
            var key = 'relation.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'relationType' :
            var key = 'relation.'+stateArray.relation+'.'+name;
            var element={};
            element[key]=sub;
            break;
        case 'relationElemValue' :
            var key = 'relation.'+stateArray.relation+'.'+stateArray.relationType+'.'+stateArray.relationElem+'.'+name;
            var element={};
            element[key]=sub;
            break;
        default : break;
    }
    return callback(id,element);
};
